<?php
if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED!==true)die();

global $APPLICATION;

$arResult["PARAMS_HASH"] = md5(serialize($arParams).$this->GetTemplateName());

if($arParams["USE_CAPTCHA"] == "Y")
{
    $arResult["capCode"] =  htmlspecialcharsbx($APPLICATION->CaptchaGetCode());
}

if($_SERVER["REQUEST_METHOD"] == "POST" && $_POST["submit"] <> '' && (!isset($_POST["PARAMS_HASH"]) || $arResult["PARAMS_HASH"] === $_POST["PARAMS_HASH"]))
{
    if($arParams["USE_CAPTCHA"] == "Y")
    {
        include_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/classes/general/captcha.php");
        $captcha_code = $_POST["captcha_sid"];
        $captcha_word = $_POST["captcha_word"];
        $cpt = new CCaptcha();
        $captchaPass = COption::GetOptionString("main", "captcha_password", "");
        if (strlen($captcha_word) > 0 && strlen($captcha_code) > 0)
        {
            if (!$cpt->CheckCodeCrypt($captcha_word, $captcha_code, $captchaPass))
            {
                $arResult["ERROR_MESSAGE"][] = GetMessage("MF_CAPTCHA_WRONG");
                $arResult["ERRORS"]['captcha_word'] = '';
            }

        }
        else
        {
            $arResult["ERROR_MESSAGE"][] = GetMessage("MF_CAPTHCA_EMPTY");
            $arResult["ERRORS"]['captcha_word'] = '';
        }
    }

    foreach($arParams["FIELDS"] as $field)
    {
        if(!is_array($field)) continue;

        if($field['valid'] == 'empty' && trim($_POST[$field['name']]) === '')
        {
            $arResult["ERROR_MESSAGE"][] = GetMessage('MF_REQ');
            $arResult["ERRORS"][$field['name']] = $field['title'];
        }
    }

    if(empty($arResult["ERROR_MESSAGE"]))
    {
        $arFields = Array(
            "EMAIL_TO" => $arParams["EMAIL_TO"]? $arParams["EMAIL_TO"] : COption::GetOptionString("main", "email_from"),
        );

        foreach($arParams["FIELDS"] as $field)
        {
            if(is_array($field))
            {
                if(LANG_CHARSET == 'windows-1251')
                {
                    $_POST[$field['name']] = iconv('utf-8', 'windows-1251',$_POST[$field['name']]);
                }
                $arFields[$field['name']] = $_POST[$field['name']];
                $arResult['FIELDS'][$field['name']] = $_POST[$field['name']];
                $arFields['FIELDS']['FIELDS'] .= $field['title'].' : '. $arResult['FIELDS'][$field['name']]."\n\n";
            }
            else
            {
                if(LANG_CHARSET == 'windows-1251')
                {
                    $_POST[$field] = iconv('utf-8', 'windows-1251',$_POST[$field]);
                }
                $arFields[$field] = $_POST[$field];
                $arResult['FIELDS'][$field] = $_POST[$field];
            }
        }

        $arFields['FORM_TITLE'] = $arParams['FORM_TITLE'];

        CEvent::SendImmediate($arParams["EVENT_NAME"], SITE_ID, $arFields);

        $arResult["OK_MESSAGE"] = $arParams["OK_TEXT"];
    }
}

$this->IncludeComponentTemplate(); 